﻿using CampWeek5__Book_Project_.Adapters.Adapters;
using CampWeek5__Book_Project_.Adapters.Interfaces;
using CampWeek5__Book_Project_.Data;
using CampWeek5__Book_Project_.Data.Model;
using CampWeek5__Book_Project_.Models;
using Microsoft.AspNet.Identity; //used for User.Identity.GetUserId();
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CampWeek5__Book_Project_.Controllers
{
    public class HomeController : Controller
    {
        private IBookAdapter _adapter;

        public HomeController()
        {
            _adapter = new BookAdapter();
        }

        public HomeController(IBookAdapter a)
        {
            _adapter = a;
        }

        public ActionResult Index()
        {
            IndexVM model = _adapter.GetIndex();
            return View(model);

           
        }

        [Authorize(Roles = Roles.ADMIN)]
        public ActionResult FeaturedBooks()
        {
            return View("SearchFeaturedBooks");
        }

        //search box for featured book
        [Authorize(Roles = Roles.ADMIN)]
        public ActionResult SearchFeaturedBooks(string isbn, string title)
        {
            if (isbn == "" && title == "") return View();
            int ISBN = 0;
            if (isbn != "")
            {
                string result = "";
                string[] isbnarr = isbn.Split('-');
                foreach (var val in isbnarr)
                {
                    result += val;
                }

                ISBN = Convert.ToInt32(result);
            }
            List<IndexBookVM> searchBooks;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                var query = db.Books.Where(x => x.DateDeleted == null);
                if (title != "") query = query.Where(x => x.Title.ToLower().Contains(title.ToLower()));
                if (isbn != "") query = query.Where(x => x.ISBN == ISBN);
                searchBooks = query.Select(x => new IndexBookVM()
               {
                   AuthorId = x.AuthorId,
                   BookImg = x.BookImg,
                   AuthorName = x.Author.AuthorName,
                   Title = x.Title,
                   ISBN = x.ISBN
               }).ToList();
            }
            return View("FeaturedBooksResults", searchBooks);
            }

        //id is the ISBN of the book
        [Authorize(Roles = Roles.ADMIN)]
        public ActionResult SelectFeaturedBook(int id)
        {
            Book model = _adapter.FindBookByISBN(id);
            Featured.FeaturedBook = new IndexBookVM()
            {
                AuthorId = model.AuthorId,
                AuthorName = model.Author.AuthorName,
                BookImg = model.BookImg,
                //BookRating = (model.BookRatings.Count > 0 ? model.BookRatings.Select(x => x.BookRating,).Average() : 0),
                BookRating = model.BookRatings.Select(x => x.BookRating).DefaultIfEmpty(0).Average(),
                ISBN = id,
                Title = model.Title,
                Summary = model.Summary
            };
            return RedirectToAction("Index");
        }
//search box
        [HttpGet]
        public ActionResult ResultsPage(string title, string isbn, string genre, string author)
        {
            if (isbn == "" && title == "" && genre == "" && author == "") return View();
            int ISBN = 0;
            if (isbn != "")
            {
                string result = "";
                string[] isbnarr = isbn.Split('-');
                foreach (var val in isbnarr)
                {
                    result += val;
                }

                ISBN = Convert.ToInt32(result);
            }
            List<IndexBookVM> searchBooks;
            List<IndexAuthorVM> searchAuthor;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                var genrequery = db.Book_Tags;
                var query = db.Books.Where(x => x.DateDeleted == null);
                var authorquery = db.Authors.Where(x => x.DateDeleted == null);
                if (author != "") authorquery = authorquery.Where(x => x.AuthorName.ToLower().Contains(author.ToLower()));
                searchAuthor = authorquery.Select(x => new IndexAuthorVM()
                        {
                            AuthorName = x.AuthorName,
                            AuthorImg = x.AuthorImg,

                        }).ToList();
                
                if (title != "") query = query.Where(x => x.Title.ToLower().Contains(title.ToLower()));
                if (isbn != "") query = query.Where(x => x.ISBN == ISBN); 

                searchBooks = query.Select(x => new IndexBookVM()
               {
                   AuthorId = x.AuthorId,
                   BookImg = x.BookImg,
                   AuthorName = x.Author.AuthorName,
                   Title = x.Title,
                   ISBN = x.ISBN
               }).ToList();                

            }
            return View("ResultsPage", searchBooks);
        }

        
        public ActionResult DisplayAuthors()
        {
            IndexVM model = new IndexVM();
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                // need to create the list of authors so this can use it
                model.Authors = db.Authors.Where(x => x.DateDeleted == null).OrderBy(x => x.AuthorName).Select(x => new IndexAuthorVM()
                {
                    AuthorId = x.Id,
                    AuthorImg = x.AuthorImg,
                    AuthorName = x.AuthorName
                }).ToList();
            }
            return View(model);
        }

        [Authorize]
        public ActionResult CreateRating(int id)
        {
            Rating model = new Rating()
            {
                BookId = id
            };

            return View(model);
        }

        [Authorize]
        [HttpPost]
        public ActionResult CreateRating(Rating model)
        {
            model.UserId = User.Identity.GetUserId();
            _adapter.CreateRating(model);
            return RedirectToAction("Index");
        }
    }
}